﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.DataVisualization.Charting;
using BawangMemo.BackendModels;

namespace BawangMemo.BawangMemoView.View
{
    /// <summary>
    /// StatView.xaml 的交互逻辑
    /// </summary>
    public partial class StatView : UserControl
    {
        private CourseWrapper course;

        public StatView(CourseWrapper course)
        {
            this.course = course;
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            int today = Int32.Parse(DateTime.Now.ToString("yyyyMMdd"));
            Summary[] summary = RecordRepository.GetRecentSummary(course, 6);

            KeyValuePair<int, int>[] items = new KeyValuePair<int, int>[7];
            for (int i = 0; i < summary.Length; i++)
            {
                if (summary[i] != null)
                {
                    items[i] = new KeyValuePair<int, int>(today - 6 + i, summary[i].WordCount);
                }
                else
                {
                    items[i] = new KeyValuePair<int, int>(today - 6 + i, 0);
                }
            }

            ((ColumnSeries)mcChart.Series[0]).ItemsSource = items;
            //    new KeyValuePair<int, int>[]
            //{
            //    new KeyValuePair<int, int>(1, 100),
            //    new KeyValuePair<int, int>(2, 130),
            //    new KeyValuePair<int, int>(3, 150),
            //    new KeyValuePair<int, int>(4, 100),
            //    new KeyValuePair<int, int>(5, 130),
            //    new KeyValuePair<int, int>(6, 150),
            //    new KeyValuePair<int, int>(7, 125)
            //};

            items = new KeyValuePair<int, int>[7];
            for (int i = 0; i < summary.Length; i++)
            {
                if (summary[i] != null)
                {
                    items[i] = new KeyValuePair<int, int>(today - 6 + i,
                        Convert.ToInt32(Math.Round(
                        summary[i].WordCount * 0.01 * summary[i].Score)));
                }
                else
                {
                    items[i] = new KeyValuePair<int, int>(today - 6 + i, 0);
                }
            }

            ((ColumnSeries)mcChart.Series[1]).ItemsSource = items;
            //    new KeyValuePair<int, int>[]
            //{
            //    new KeyValuePair<int, int>(1, 80),
            //    new KeyValuePair<int, int>(2, 150),
            //    new KeyValuePair<int, int>(3, 140),
            //    new KeyValuePair<int, int>(4, 150),
            //    new KeyValuePair<int, int>(5, 140),
            //    new KeyValuePair<int, int>(6, 105),
            //    new KeyValuePair<int, int>(7, 105)
            //};

            int listCount = ListRepository.GetListCount(course);
            for (int i = 1; i <= listCount; i++)
                listBox1.Items.Add("List " + i);
        }

        private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int listId = listBox1.SelectedIndex + 1;
            ListStat stat = new ListStat(course, listId);
            this.label1.Content = "List " + listId;
            this.label2.Content = "From: " + stat.StartWord;
            this.label3.Content = "To: " + stat.EndWord;
            this.statRateFive.Count = stat.FiveStarCount;
            this.statRateFive.BackendList = stat.FiveStarList;
            this.statRateFour.Count = stat.FourStarCount;
            this.statRateFour.BackendList = stat.FourStarList;
            this.statRateThree.Count = stat.ThreeStarCount;
            this.statRateThree.BackendList = stat.ThreeStarList;
            this.statRateTwo.Count = stat.TwoStarCount;
            this.statRateTwo.BackendList = stat.TwoStarList;
            this.statRateOne.Count = stat.OneStarCount;
            this.statRateOne.BackendList = stat.OneStarList;
        }
    }
}
